<template>
  <div>
    <page-list :x-uri="uri" :custom-components="custom" :doactions="doactions">
      <!-- 自定义tab -->
      <template v-slot:page-tabs>
        <tabs />
      </template>
      <!-- 页面操作按钮 -->
      <template v-slot:page-button>
        <el-button v-auth="'course.video.default.create'" size="mini" type="primary" @click="create">新建点播</el-button>
      </template>
      <!-- 多选操作 -->
      <template v-slot:table-button="scope">
        <el-button v-auth="'course.video.default.edit'" size="mini" @click="listing(scope)">上架</el-button>
        <el-button v-auth="'course.video.default.edit'" size="mini" @click="disable(scope)">下架</el-button>
        <el-button v-auth="'course.video.default.delete'" size="mini" @click="deletes(scope)">删除</el-button>
      </template>
    </page-list>
    <sort-dialog :value.sync="sortValue" :visible.sync="visible" :disable-submit="disableSubmit" @submit="onSubmit" />
    <!-- 弹题题库配置 start -->
    <choose-question-bank v-if="examConfigVisible" :visible.sync="examConfigVisible" data-type="video" :data-id="popupId" />
    <!-- 弹题题库配置 end-->
  </div>
</template>
<script>
import PageList from '@public/template/page_list.vue';
import Status from './components/status';
import UpdateStatus from './components/update_status';
import Titile from './components/title.vue';
import IsRecommend from './components/is_recommend.vue';
import CreditRedeem from './components/credit_redeem.vue';
import Tabs from './components/tabs.vue';
import SortDialog from '@/components/update-sort-dialog';
import ChooseQuestionBank from '@/pages/exam/components/ChooseQuestionBank'
import CategoryTitle from '@/components/category-title'

export default {
  components: {
    PageList,
    Tabs,
    SortDialog,
    ChooseQuestionBank
  },
  inject: ['reload'],
  data() {
    return {
      uri: 'course/admin/video/list',
      custom: {
        update_status: UpdateStatus,
        status: Status,
        title: Titile,
        is_recommend: IsRecommend,
        credit_redeem: CreditRedeem,
        cates: CategoryTitle
      },
      doactions: [
        {
          title: '课时管理',
          meta: { permission: 'course.video.default.section' },
          callback: this.section
        },
        {
          title: '学员',
          meta: { permission: 'course.video.default.student' },
          callback: this.student
        },
        {
          title: '课件',
          meta: { permission: 'course.video.default.courseware' },
          callback: this.courseware
        },
        {
          title: '排序',
          meta: { permission: 'course.video.default.order' },
          callback: this.sortPopover
        },
        {
          title: '编辑',
          meta: { permission: 'course.video.default.edit' },
          callback: this.edit
        },
        {
          title: '代理',
          meta: { permission: 'course.video.default.agent' },
          callback: this.agent
        },
        {
          title: '弹题',
          meta: { permission: 'course.video.default.popup' },
          callback: this.onhook
        },
        {
          title: '考试',
          meta: { permission: 'course.video.default.exam' },
          callback: this.exam
        },
        {
          title: '证书',
          meta: { permission: 'course.video.default.cert' },
          callback: this.cert
        },
        {
          title: '评论',
          meta: { permission: 'course.video.default.comment' },
          callback: this.comment
        },
        {
          title: '笔记',
          meta: { permission: 'course.video.default.note' },
          callback: this.note
        },
        {
          title: '收藏',
          meta: { permission: 'course.video.default.collect' },
          callback: this.collection
        },
        {
          title: '加购',
          meta: { permission: 'course.video.default.present' },
          callback: this.cart
        },
        {
          title: '复制',
          meta: { permission: 'course.video.default.copy' },
          callback: this.copyCourse
        },
        {
          title: '删除',
          meta: { permission: 'course.video.default.delete' },
          callback: this.delete
        }
      ],
      itemId: 0,
      visible: false,
      disableSubmit: false,
      sortValue: 1, // 排序值
      examConfigVisible: false, // 弹题题库选择弹出层
      popupId: 0 // 弹题设置的数据id
    };
  },
  methods: {
    create: function() {
      this.$router.push({ path: '/course/video/create' });
    },
    // 证书
    cert: function({ row }) {
      this.$router.push({ path: '/course/cert/' + row.id + '/student' });
    },
    // 课件
    courseware: function({ row }) {
      this.$router.push({ path: '/course/courseware/' + row.id + '/list' });
    },
    // 考试
    exam: function({ row }) {
      this.$router.push({
        path: '/course/exam/' + row.id + '/list',
        query: { type: 'video' }
      });
    },
    // 评论
    comment: function({ row }) {
      this.$router.push({ path: '/course/comment/' + row.id + '/list' });
    },
    // 笔记
    note: function({ row }) {
      this.$router.push({ path: '/course/note/' + row.id + '/list' });
    },
    // 收藏
    collection: function({ row }) {
      this.$router.push({ path: '/course/collection/' + row.id + '/list' });
    },
    // 加购
    cart: function({ row }) {
      this.$router.push({ path: '/course/cart/' + row.id + '/list' });
    },
    // 复制课程
    copyCourse: function({ row }) {
      this.$box
        .confirm('确认复制课程信息吗')
        .then(() => {
          this.$http.post(
            '/course/admin/' + row.id + '/copy',
            {},
            response => {
              this.$message.success(response.msg, _ => {
                this.reload();
              });
            },
            error => {
              this.$message.error(error.msg);
            }
          );
        })
        .catch(() => {});
    },
    // 课时管理
    section: function({ row }) {
      this.$router.push({ path: '/course/video/' + row.id + '/section' });
    },
    // 编辑
    edit: function({ row }) {
      this.$router.push({ path: '/course/video/' + row.id + '/edit' });
    },
    // 代理
    agent: function({ row }) {
      // 错误提示
      if (row.mhm_id > 1 || row.status == 0 || row.solo == 0) {
        this.$message.warning('该课程不满足代理条件，仅平台课程才能代理');
        return false;
      }
      this.$router.push({ path: '/course/agent/' + row.id + '/info' });
    },
    // 弹题
    onhook({ row }) {
      this.popupId = row.id
      this.examConfigVisible = true
    },
    // 删除
    delete: function({ row }) {
      this.$box
        .confirm('确认删除吗')
        .then(() => {
          this.$http.delete(
            '/course/admin/video/' + row.id,
            {},
            response => {
              this.$message.success(response.msg, _ => {
                this.reload();
              });
            },
            error => {
              this.$message.error(error.msg);
            }
          );
        })
        .catch(() => {});
    },
    student: function({ row }) {
      this.$router.push({ path: '/course/video/' + row.id + '/student' });
    },
    // 批量上架课程
    listing: function({ rows }) {
      var ids = rows.map(row => row.id);
      if (ids.length === 0) {
        this.$message.warning('请选择要上架的课程');
        return false;
      }
      this.$box
        .confirm('确认上架吗')
        .then(() => {
          this.$http.put(
            '/course/admin/video/listing',
            {
              ids: ids
            },
            response => {
              this.$message.success(response.msg, _ => {
                this.reload();
              });
            },
            error => {
              this.$message.error(error.msg);
            }
          );
        })
        .catch(() => {});
    },
    // 批量禁用课程
    disable: function({ rows }) {
      var ids = rows.map(row => row.id);
      if (ids.length === 0) {
        this.$message.warning('请选择要禁用的课程');
        return false;
      }
      this.$box
        .confirm('确认禁用吗')
        .then(() => {
          this.$http.put(
            '/course/admin/video/disables',
            {
              ids: ids
            },
            response => {
              this.$message.success(response.msg, _ => {
                this.reload();
              });
            },
            error => {
              this.$message.error(error.msg);
            }
          );
        })
        .catch(() => {});
    },
    // 批量删除课程
    deletes: function({ rows }) {
      var ids = rows.map(row => row.id);
      if (ids.length === 0) {
        this.$message.warning('请选择要删除的课程');
        return false;
      }
      ids = ids.join(',');
      this.$box
        .confirm('确认删除吗')
        .then(() => {
          this.$http.delete(
            '/course/admin/video/deletes/' + ids,
            {},
            response => {
              this.$message.success(response.msg, _ => {
                this.reload();
              });
            },
            error => {
              this.$message.error(error.msg);
            }
          );
        })
        .catch(() => {});
    },
    // 排序弹出层
    sortPopover({ row }) {
      this.visible = true;
      this.itemId = row.id;
      this.sortValue = row.best_sort;
    },
    // 保存
    onSubmit() {
      this.disableSubmit = true;
      this.$http.put(
        '/course/admin/video/' + this.itemId + '/edit/best_sort',
        {
          best_sort: this.sortValue
        },
        ({ msg }) => {
          this.$message.success(msg);
          this.disableSubmit = false;
          this.visible = false;
          this.reload();
        },
        ({ msg }) => {
          this.$message.error(msg);
          this.disableSubmit = false;
        },
        false
      );
    }
  }
};
</script>
